<?php
/**
 * Ver. PHP 5
 * @package: db abstraction class
 * @author: Web Analytics
 * @date: 03/01/2011
 */
class MySQLDB {
    //private vars
    private $a_conn;
    private $is_persistent;

    //protected vars
    protected $dbhost;
    protected $dbusername;
    protected $dbpassword;
    protected $dbname;
	protected $all_queries;
	protected $cnt_queries;
	protected $no_rows;	
	
    /**
     * @desc: constructor
     * @access: public
     * @param: string hotname
     * @param: string username
     * @param: string password
     * @param: string db anme
     * @param: boolean return connection
     * @param: boolean persistent connection
     * @return: void
     */
    public function __construct($host, $user, $pwd, $db, $a_conn = false, $is_p = false) {
        $this->dbhost = $host;
        $this->dbusername = $user;
        $this->dbpassword = $pwd;
        $this->dbname = $db;
        $this->a_conn = $a_conn;
        $this->is_persistent = $is_p;
        $this->all_queries = array();
		$this->cnt_queries = 0;
		$this->no_rows = 0;
		
        //call connection
        $this->MySQLDB_Connect();

        //call database selected
        $this->MySQLDB_Select();
    } //end function
	
	/**
     * @desc: destructor
     * @access: public
     * @return: void
     */
    public function __destruct() {
        $this->dbhost = '';
        $this->dbusername = '';
        $this->dbpassword = '';
        $this->dbname = '';
        $this->a_conn = '';
        $this->is_persistent = '';
        $this->all_queries = array();
		$this->cnt_queries = 0;
		$this->no_rows = 0;
		
		@mysql_close();
    } //end function

    /**
     * @desc: set connection
     * @access: private
     * @return: boolean
     */
    private function MySQLDB_Connect() {
        if ($this->is_persistent == true){
            return @mysql_pconnect($this->dbhost, $this->dbusername, $this->dbpassword, $this->a_conn) or $this->MySQLDB_Error('DB Conmnection');    
		}else{
            return @mysql_connect($this->dbhost, $this->dbusername, $this->dbpassword, $this->a_conn) or $this->MySQLDB_Error('DB Conmnection');    
		}	
    } //end function

    /**
     * @desc: set database
     * @access: private
     * @return: boolean
     */
    private function MySQLDB_Select() {
        return @mysql_select_db($this->dbname) or $this->MySQLDB_Error( 'Can\'t use '.$proj_config['db'].' : ' );    
    } //end function
	
	
	
	/**
     * @desc: debuggin state
     * @access: private
	 * @param: string message
     * @return: boolean
     */
    public function MySQLDB_Debug($msg = ' -- ') {
        $err = '';
		$err .= '<div style="background-color:#FFFFDD;border:1px solid #FFD700;color:#000000 !important;padding:5px 10px 10px;width:80%;">
					<h4 style="font-size:15px !important;font-weight:bold !important;padding:2px 0 5px;">MYSQL Debug:</h4>
					<ul style="font-size:14px !important;">
						<li>Info: '.$msg.'</li>
						<li>Client Info: '. mysql_get_client_info() .'</li>
						<li>Process: '. mysql_list_processes() .'</li>
						<li>All Queries: '.$this->cnt_queries.' </li>
					</ul>
					';
		if($this->cnt_queries > 0){	
			$err .= '<ul style="padding-left:10px;font-size:14px !important;"><li>--> '.implode('</li><li>--> ', $this->all_queries).'</li></ul>';				
		}		
		$err .= '</div>';
		
		if(DEBUGMODE == true){
			echo $err;		
		}	
		
    } //end function
	
	/**
     * @desc: set mysql error
     * @access: private
	 * @param: string message
     * @return: boolean
     */
    public function MySQLDB_Error($msg = '') {
        $err = '';
		$err .= '<div style="background-color:#FFEFEF;border:1px solid #FF0000;color:#D73535 !important;padding:5px 10px 10px;width:80%;">
					<h4 style="font-size:15px !important;font-weight:bold !important;padding:2px 0 5px;">MYSQL Error:</h4>
					<ul style="font-size:13px !important;">
						<li>Info: '.$msg.'</li>
						<li>Error No: '. mysql_errno() .'</li>
						<li>Error: '.mysql_error().'</li>
												
						<li>All Queries: '.$this->cnt_queries.' </li>
					</ul>
					';
		if($this->cnt_queries > 0){	
			$err .= '<ul style="padding-left:10px;font-size:13px !important;"><li>--> '.implode('</li><li>--> ', $this->all_queries).'</li></ul>';				
		}		
		$err .= '</div>';
		die($err);		
		
    } //end function
	
    /**
     * @desc: execute query
     * @access: public
     * @param: string sql query
     * @return: boolean
     */
    public function MySQLDB_Query($sql) {
		$this->cnt_queries++; 
		$this->all_queries[] = $sql;
        $query = @mysql_query($sql) or $this->MySQLDB_Error( 'Sql Query Error: '.$sql );
		if(preg_match('/^select/i',$sql)){
			$this->no_rows = @mysql_num_rows($query);			
		}else{
			$this->no_rows = @mysql_affected_rows();
		}	
        return $query;
    } //end function

	/**
     * @desc: get num rows
     * @access: public
     * @return: boolean
     */
    public function MySQLDB_Numrows() {
        return $this->no_rows;
    } //end function

    /**
     * @desc: retrieve data
     * @access: public
     * @param: string sql query
     * @param: string (ARRAY, ROW)
     * @return: array
     */
    public function MySQLDB_GetData($sql, $type = 'ROW') {
        $row = array();
        $ret = array();
        $query = $this->MySQLDB_Query($sql);
        if (count($query) > 0 && $this->no_rows > 0) {
            while ($row = mysql_fetch_assoc($query)) {
                if ($type == "ARRAY")
                    $ret[] = $row;        
                elseif ($type == "ROW") {
                    $ret = $row;
                    break;
                }
            } //end while
        } //end if
		//only for select query free result
		//for insert/update/delete don't free result
		$this->MySQLDB_Free($query);
        return $ret;
    } //end function	

    /**
     * @desc: execute query
     * @access: public
     * @param: string sql query
     * @return: int
     */
    public function MySQLDB_Insert($sql) {
        $this->MySQLDB_Query($sql);
        return mysql_insert_id();
    } //end function
    
	/**
     * @desc: free result set
     * @access: public
     * @param: string resource id
     * @return: NA
     */
    public function MySQLDB_Free($result) {		
		// Free the resources associated with the result set
		// This is done automatically at the end of the script
		@mysql_free_result($result);	
    } //end function
	
    /**
     * @desc: properly quote data
     * @access: public
     * @param: string
     * @return: string
     */
    public function MySQLDB_Sanitize($data) {
        if (get_magic_quotes_gpc()) 
            $data = stripslashes(trim($data));

        if (!is_numeric($data)) 
            $data = mysql_real_escape_string($data);

        return $data;
    } //end function

}//end class
?>